package com.qyp.core;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.Font;

public class MyWindow {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyWindow window = new MyWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MyWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("简易计算器");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		//
		EmptyBorder emBorder = new EmptyBorder(5, 10, 5, 10);
		textField = new JTextField();
		textField.setBounds(167, 16, 130, 26);
		textField.setColumns(10);
//		textField.setBorder(emBorder);
//		textField.setBorder(new LineBorder(Color.RED, 1));
		// 保留原有内边距，只改变外边框颜色
		textField.setBorder(new CompoundBorder(new LineBorder(Color.BLUE, 1), // 外边框(蓝色)
				new EmptyBorder(5, 10, 5, 10) // 内边距
		));
		frame.getContentPane().add(textField);

		textField_1 = new JTextField();
		textField_1.setBounds(167, 54, 130, 26);
		textField_1.setBorder(emBorder);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);

		JButton btnNewButton = new JButton("计算求和");
		btnNewButton.setFont(new Font("苹方 中等", Font.PLAIN, 16));
		btnNewButton.setBackground(new Color(62, 110, 255));
		btnNewButton.setBounds(167, 96, 130, 39);
		// btnNewButton.setBorder(emBorder);
		frame.getContentPane().add(btnNewButton);

		textField_2 = new JTextField();
		textField_2.setEditable(false);
		textField_2.setHorizontalAlignment(SwingConstants.CENTER);
		textField_2.setBounds(167, 147, 130, 26);
		textField_2.setBorder(emBorder);
		textField_2.setColumns(10);
		btnNewButton.setBackground(new Color(255, 255, 255));
		frame.getContentPane().add(textField_2);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str1 = textField.getText();
				String str2 = textField_1.getText();
				if ("".equals(str1)) {
					textField.setBorder(new CompoundBorder(new LineBorder(Color.red, 1), // 外边框(蓝色)
							new EmptyBorder(5, 10, 5, 10) // 内边距
					));
//					return;
				}
				if ("".equals(str2)) {
					textField_1.setBorder(new CompoundBorder(new LineBorder(Color.red, 1), // 外边框(蓝色)
							new EmptyBorder(5, 10, 5, 10) // 内边距
					));
					return;
				}

				int num1 = Integer.parseInt(str1);
				int num2 = Integer.parseInt(str2);
				int sum = num1 + num2;
				textField_2.setText(sum + "");
			}
		});
	}
}
